package com.et114.core.utility;

/*
 * Copyright (c) 2005 Allways, LLC. All rights reserved.
 * �������� 2005-11-22
 * ���ù����๹����
 */

/**
 * ���У�鳣�ù����� @ <a href="Validator.java.html"><b><i>View Source</i></b></a>
 * 
 * @���� daomingw
 * @�汾 1.0
 */
public class Validator {
	
	/**
	 * �����ַ��Ƿ��ǵ�ַ
	 * 
	 * @param address
	 * @return
	 */
	
	public static boolean isAddress ( String address ) {
		if ( isNull ( address ) ) {
			return false;
		}
		
		char [ ] c = address.toCharArray ( );
		
		for ( int i = 0 ; i < c.length ; i ++ ) {
			if ( ( ! isChar ( c [ i ] ) ) && ( ! isDigit ( c [ i ] ) )
					&& ( ! Character.isWhitespace ( c [ i ] ) ) ) {
				
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * У���Ƿ����ַ�
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isChar ( char c ) {
		return Character.isLetter ( c );
	}
	
	/**
	 * �����ַ��Ƿ��(�ַ�
	 * 
	 * @param s
	 * @return
	 */
	
	public static boolean isChar ( String s ) {
		if ( isNull ( s ) ) {
			return false;
		}
		
		char [ ] c = s.toCharArray ( );
		
		for ( int i = 0 ; i < c.length ; i ++ ) {
			if ( ! isChar ( c [ i ] ) ) {
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * ����ַ��Ƿ�������
	 * 
	 * @param c
	 * @return
	 */
	public static boolean isDigit ( char c ) {
		int x = ( int ) c;
		
		if ( ( x >= 48 ) && ( x <= 57 ) ) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * ����ַ��Ƿ����������
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isDigit ( String s ) {
		if ( isNull ( s ) ) {
			return false;
		}
		
		char [ ] c = s.toCharArray ( );
		
		for ( int i = 0 ; i < c.length ; i ++ ) {
			if ( ! isDigit ( c [ i ] ) ) {
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * �ж��Ƿ�Ϊ����
	 * 
	 * @param month
	 * @param day
	 * @param year
	 * @return
	 */
	public static boolean isDate ( int month , int day , int year ) {
		return CalendarUtil.isDate ( month , day , year );
	}
	
	/**
	 * �Ƿ���Email��ַ
	 * 
	 * @param ea
	 * @return
	 */
	public static boolean isEmailAddress ( String ea ) {
		if ( isNull ( ea ) ) {
			return false;
		}
		
		int eaLength = ea.length ( );
		
		if ( eaLength < 6 ) {
			
			// j@j.c
			
			return false;
		}
		
		ea = ea.toLowerCase ( );
		
		int at = ea.indexOf ( '@' );
		
		// Unix based email addresses cannot be longer than 24 characters.
		// However, many Windows based email addresses can be longer than 24,
		// so we will set the maximum at 48.
		
		// int maxEmailLength = 24;
		int maxEmailLength = 48;
		
		if ( ( at > maxEmailLength ) || ( at == - 1 ) || ( at == 0 )
				|| ( ( at <= eaLength ) && ( at > eaLength - 5 ) ) ) {
			
			// 123456789012345678901234@joe.com
			// joe.com
			// @joe.com
			// joe@joe
			// joe@jo
			// joe@j
			
			return false;
		}
		
		int dot = ea.lastIndexOf ( '.' );
		
		if ( ( dot == - 1 ) || ( dot < at ) || ( dot > eaLength - 3 ) ) {
			
			// joe@joecom
			// joe.@joecom
			// joe@joe.c
			
			return false;
		}
		
		if ( ea.indexOf ( ".." ) != - 1 ) {
			
			// joe@joe..com
			
			return false;
		}
		
		char [ ] name = ea.substring ( 0 , at ).toCharArray ( );
		
		for ( int i = 0 ; i < name.length ; i ++ ) {
			if ( ( ! isChar ( name [ i ] ) ) && ( ! isDigit ( name [ i ] ) )
					&& ( name [ i ] != '.' ) && ( name [ i ] != '-' )
					&& ( name [ i ] != '_' ) ) {
				
				return false;
			}
		}
		
		if ( ( name [ 0 ] == '.' ) || ( name [ name.length - 1 ] == '.' )
				|| ( name [ 0 ] == '-' ) || ( name [ name.length - 1 ] == '-' )
				|| ( name [ 0 ] == '_' ) ) { // || (name[name.length - 1] ==
												// '_')) {
		
			// .joe.@joe.com
			// -joe-@joe.com
			// _joe_@joe.com
			
			return false;
		}
		
		char [ ] host = ea.substring ( at + 1 , ea.length ( ) ).toCharArray ( );
		
		for ( int i = 0 ; i < host.length ; i ++ ) {
			if ( ( ! isChar ( host [ i ] ) ) && ( ! isDigit ( host [ i ] ) )
					&& ( host [ i ] != '.' ) && ( host [ i ] != '-' ) ) {
				
				return false;
			}
		}
		
		if ( ( host [ 0 ] == '.' ) || ( host [ host.length - 1 ] == '.' )
				|| ( host [ 0 ] == '-' ) || ( host [ host.length - 1 ] == '-' ) ) {
			
			// joe@.joe.com.
			// joe@-joe.com-
			
			return false;
		}
		
		// postmaster@joe.com
		
		if ( ea.startsWith ( "postmaster@" ) ) {
			return false;
		}
		
		// root@.com
		
		if ( ea.startsWith ( "root@" ) ) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * ����Ƿ�������
	 * 
	 * @param number
	 * @return
	 */
	public static boolean isNumber ( String number ) {
		if ( isNull ( number ) ) {
			return false;
		}
		
		char [ ] c = number.toCharArray ( );
		
		for ( int i = 0 ; i < c.length ; i ++ ) {
			if ( ! isDigit ( c [ i ] ) ) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * ����ַ��Ƿ��ǿջ���ַ�
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isNull ( String s ) {
		if ( s == null ) {
			return true;
		}
		
		s = s.trim ( );
		
		if ( ( s.equals ( StringPool.NULL ) )
				|| ( s.equals ( StringPool.BLANK ) )
				|| s.endsWith ( StringPool.SBCBLANK )
				|| s.startsWith ( StringPool.SBCBLANK ) ) {
			return true;
		}
		
		return false;
	}
	
	public static boolean isNotNull ( String s ) {
		return ! isNull ( s );
	}
	
	/**
	 * �����Ƿ��ǵ绰����
	 * 
	 * @param phoneNumber
	 * @return
	 */
	public static boolean isPhoneNumber ( String phoneNumber ) {
		if ( phoneNumber.indexOf ( "+" ) == 0 )
			phoneNumber = phoneNumber.substring ( 1 , phoneNumber.length ( ) );
		if ( phoneNumber.indexOf ( "-" ) != - 1 )
			phoneNumber = phoneNumber.replaceAll ( "-" , "" );
		if ( phoneNumber.indexOf ( "*" ) != - 1 ) {
			phoneNumber = phoneNumber.replace ( '*' , ' ' );
			phoneNumber = phoneNumber.replaceAll ( " " , "" );
		}
		
		if ( phoneNumber.indexOf ( "#" ) != - 1 )
			phoneNumber = phoneNumber.replaceAll ( "#" , "" );
		return isNumber ( phoneNumber );
	}
	
	/**
	 * �ж��Ƿ�Ϊ���
	 * 
	 * @param money
	 * @return
	 */
	public static boolean isMoney ( String money ) {
		if ( null == money ) {
			return false;
		}
		try {
			new Double ( money );
		} catch ( NumberFormatException e ) {
			return false;
		}
		return true;
	}
	
	/**
	 * �ж��ַ����Ƿ�С�ڵ�������ĳ���
	 * 
	 * @param value
	 * @param length
	 * @return
	 */
	public static boolean isShortThan ( String value , int length ) {
		if ( isNull ( value ) )
			return true;
		byte [ ] bytes = value.getBytes ( );
		if ( bytes.length > length )
			return false;
		return true;
	}
	
	public static void main ( String [ ] args ) {
		System.out.println ( isPhoneNumber ( "+861111111111" ) );
	}
}
